/**********************************************************
*
*   @file    useri2cReg.c
*   @author  Derrick Wang
*   @time    2018-4-26
*		@brief   i2c协议和外部交互的函数
*
*
***********************************************************/


#include "main.h"
#include "stm32f0xx_hal.h"
#include "userI2c.h"
#include "userI2cReg.h"
#include "lcd1602.h"


struct userI2cRegStruct userI2cRegList[]=
{
	//通用寄存器
	{REG_CHECK,1,CMD_RT_TRANSMIT},
	{REG_VERSION,1,CMD_RT_TRANSMIT},
	{REG_LED,1,CMD_RT_RECEIVE},
	{REG_RESET,0,CMD_RT_RECEIVE},
	//模块专用寄存器
	{REG_CLEAN_SCREEN,0,CMD_RT_RECEIVE},
	{REG_LINE1_NORMAL,1,CMD_RT_RECEIVE},
	{REG_LINE2_NORMAL,1,CMD_RT_RECEIVE},
	{REG_LINE1_ROLL,1,CMD_RT_RECEIVE},
	{REG_LINE2_ROLL,1,CMD_RT_RECEIVE},
	{REG_BACK_LIGHT,1,CMD_RT_RECEIVE},
	{REG_DIS_VERSION,1,CMD_RT_RECEIVE}
};

//获取要发送的数据，返回-1为错误，其他值为返回的数据长度
//cmd为命令，buf用来存放获取到的数据
uint8_t user_I2cGetTransmitData(uint8_t reg,uint8_t *buf)
{
	switch(reg)
	{
		case REG_CHECK:
			buf[0]=REG_CHECK_REPLY;
			break;
		case REG_VERSION:
			buf[0]=VERSION;
			break;
	}
	return 1;
}
//命令执行函数
void user_I2cExecuteCallBack(uint8_t reg,uint8_t *buf,uint8_t size)
{
	switch(reg)
	{
		case REG_LED:
			if(buf[0]==0x00)
			{
				LED_OFF();
			}
			else
			{
				LED_ON();
			}
			break;
		case REG_LINE1_NORMAL:
			user_Lcd1602SendMail(LCD_LINE1,LCD_DIS_MODE_NORMAL,buf,size);
			break;
		case REG_LINE2_NORMAL:
			user_Lcd1602SendMail(LCD_LINE2,LCD_DIS_MODE_NORMAL,buf,size);
			break;			
		case REG_LINE1_ROLL:
			user_Lcd1602SendMail(LCD_LINE1,LCD_DIS_MODE_ROLL,buf,size);
			break;
		case REG_LINE2_ROLL:
			user_Lcd1602SendMail(LCD_LINE2,LCD_DIS_MODE_ROLL,buf,size);
			break;
		case REG_CLEAN_SCREEN:
			user_Lcd1602SendMail(LCD_LINE_NONE,NULL,NULL,1); 
			break;
		case REG_BACK_LIGHT:
			user_Lcd1602BackLight(buf[0]);
			break;
		case REG_RESET:
			NVIC_SystemReset();
			break;
	}
}
